//
//  MainScrollTbv.m
//  ZITHER
//
//  Created by weixin on 2018/8/27.
//  Copyright © 2018年 weixn. All rights reserved.
//

#import "MainScrollTbv.h"
#import "MainDefaultTbCell.h"
#import "MainQutoeCell.h"



#import "CustomCameraVC.h"
#import "STViewController.h"

@implementation MainScrollTbv

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.delegate = self;
        self.dataSource = self;
        self.separatorStyle = UITableViewCellSeparatorStyleNone;
        self.showsHorizontalScrollIndicator = NO;
        self.showsVerticalScrollIndicator = NO;
        
        [self registerNib:[UINib nibWithNibName:@"MainFirstImageCell" bundle:nil] forCellReuseIdentifier:@"MainFirstImageCell"];
        [self registerNib:[UINib nibWithNibName:@"MainDefaultTbCell" bundle:nil] forCellReuseIdentifier:@"MainDefaultTbCell"];
        [self registerNib:[UINib nibWithNibName:@"MainQutoeCell" bundle:nil] forCellReuseIdentifier:@"MainQutoeCell"];
        
    }
    return self;
}

-(void)setArrM:(NSMutableArray *)arrM
{
    _arrM = arrM;
    
    [self reloadData];
    
}

#pragma mark -- tabdelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //    return self.arrM.count;
    if (self.index == 0){
        
        MyEnquiryListModel *model = self.arrMSource[0];
        
        NSArray *arr = self.arrMSource[1];
        
        if ( [[self.arrMSource[0] class] isEqual:[MyEnquiryListModel class]]) {
            return model.data.count+arr.count;
        }
        
        return arr.count;
        
    }else{
        NSArray *arr = self.arrMSource[self.index+1];
        return arr.count;
    }
    
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.index == 0){
        static NSString *sectionCell = @"MainQutoeCell";
        MainQutoeCell * cell = [tableView dequeueReusableCellWithIdentifier:sectionCell];
        if (cell == nil) {
            cell = [[MainQutoeCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:sectionCell];
        }
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        MyEnquiryListModel *model = self.arrMSource[0];
        
        
        //是否有正在询价数据
        //如果有先展示询价数据
        if ( [[self.arrMSource[0] class] isEqual:[MyEnquiryListModel class]]) {
            //判断是否当前是否超过询价数据个数 超过展示一结束询价
            if (indexPath.row < model.data.count) {
                
                cell.modelType2 = model.data[indexPath.row];
                
            }else{
                
                NSArray *arr = self.arrMSource[1];
                
                cell.modelType2 = arr[indexPath.row-model.data.count];
                
            }
        }else{
            //没有当前询价数据   直接展示结束询价数据
            NSArray *arr = self.arrMSource[1];
            
            cell.modelType2 = arr[indexPath.row];
        }
        
        
        return cell;
    }else{
        static NSString *sectionCell = @"MainDefaultTbCell";
        MainDefaultTbCell * cell = [tableView dequeueReusableCellWithIdentifier:sectionCell];
        if (cell == nil) {
            cell = [[MainDefaultTbCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:sectionCell];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        if (self.index == 1) {
            cell.followModel = self.arrMSource[2][indexPath.row];
        }else{
            cell.model = self.arrMSource[3][indexPath.row];
        }
        
        return cell;
    }
    
}



-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if(self.index == 0){
        
        MyEnquiryListModel *model = self.arrMSource[0];
        
        STViewController *vc = [[STViewController alloc]init];
        vc.type = STControllerTypeHiddenNavBar;
        
//        SKUDetailNewVc *vc = [[SKUDetailNewVc alloc]init];
        
        //是否有正在询价数据
        //如果有先展示询价数据
        if ( [[self.arrMSource[0] class] isEqual:[MyEnquiryListModel class]]) {
            //判断是否当前是否超 过询价数据个数 超过展示一结束询价
            if (indexPath.row < model.data.count) {
                MyEnquiryListDataModel * model1 = model.data[indexPath.row];
                vc.goodId = model1.userEnquiryInfo.goodsId;
//                vc.userEnquiryId = model1.userEnquiryInfo.userEnquiryId;
            }else{
                NSArray *arr = self.arrMSource[1];
                MyEnquiryListDataModel * model1 = arr[indexPath.row - model.data.count];
                vc.goodId = model1.userEnquiryInfo.goodsId;
                }
        }else{
            //没有当前询价数据   直接展示结束询价数据
            NSArray *arr = self.arrMSource[1];
            MyEnquiryListDataModel * model = arr[indexPath.row];
            vc.goodId = model.userEnquiryInfo.goodsId;
        }
        
        [self.getNav pushViewController:vc animated:YES];

    }else{
        
        if (self.index == 1) {
            FollowGoodsModel *model = self.arrMSource[2][indexPath.row];
            STViewController *vc = [[STViewController alloc]init];
            vc.type = STControllerTypeHiddenNavBar;
            vc.goodId = model.goodsId;
            [self.getNav pushViewController:vc animated:YES];
        }else{
            goodVOsModel *model = self.arrMSource[3][indexPath.row];
            STViewController *vc = [[STViewController alloc]init];
            vc.type = STControllerTypeHiddenNavBar;
            vc.goodId = model.goodInfo.id;
            [self.getNav pushViewController:vc animated:YES];
        }
        
        
    }
    
}



- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewAutomaticDimension;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //    NSLog(@"mycellHeight = %f",mycellHeight+10);
    
    if (self.index == 0){
        
        CGFloat height = 0;
        
        CGFloat userH = 0;
        
        MyEnquiryListModel *model = self.arrMSource[0];
        
        //是否有正在询价数据
        //如果有先展示询价数据
        if ( [[self.arrMSource[0] class] isEqual:[MyEnquiryListModel class]]) {
            //判断是否当前是否超过询价数据个数 超过展示一结束询价
            if (indexPath.row < model.data.count) {
                
                MyEnquiryListDataModel *model1 = model.data[indexPath.row];
                if (![model1.freshPriceCount isEqualToString:@"0"]) {
                    height +=40;
                }
                
                if (![model1.openSourceCount isEqualToString:@"0"]) {
                    height +=40;
                }
                
                if (![model1.userEnquiryInfo.refreshTime isEqualToString:@"0"]) {
                    height +=40;
                }
                
                if (model1.bizInfoList.count>0) {
                    userH = 60;
                }
                
                return 100+height+userH;
            }else{
                
                NSArray *arr = self.arrMSource[1];
                
                MyEnquiryListDataModel *model1  = arr[indexPath.row-model.data.count];
                if (![model1.freshPriceCount isEqualToString:@"0"]) {
                    height +=40;
                }
                
                if (![model1.openSourceCount isEqualToString:@"0"]) {
                    height +=40;
                }
                
                if (![model1.userEnquiryInfo.refreshTime isEqualToString:@"0"]) {
                    height +=40;
                }
                
                if (model1.bizInfoList.count>0) {
                    userH = 60;
                }
                
                return 100+height+userH;
            }
        }else{
            //没有当前询价数据   直接展示结束询价数据
            NSArray *arr = self.arrMSource[1];
            
            MyEnquiryListDataModel *model1 = arr[indexPath.row];
            if (![model1.freshPriceCount isEqualToString:@"0"]) {
                height +=40;
            }
            if (![model1.openSourceCount isEqualToString:@"0"]) {
                height +=40;
                
            }
            if (![model1.userEnquiryInfo.refreshTime isEqualToString:@"0"]) {
                height +=40;
            }
            if (model1.bizInfoList.count>0) {
                userH = 60;
            }
            return 100+height+userH;
        }
        
        
        
    }else if (self.index == 2){
        return 180;
    }else{
        return 180;
    }
    
    //数据齐全
    
    
}

-(void)setIndex:(NSInteger)index
{
    _index = index;
    NSLog(@"index ========= %ld",index);
    //    [self.subviews.su reloadData];
}


-(NSMutableArray *)arrMSource{
    if (!_arrMSource) {
        _arrMSource = [[NSMutableArray alloc]init];
        for (int i =0; i<4; i++) {
            [_arrMSource addObject:[[NSMutableArray alloc]init] ];
        }
    }
    return _arrMSource;
}
@end
